<?php

namespace app\api\service\pay;

use app\api\model\business\PurchaseRecords;
use app\common\enum\order\OrderStatus;
use app\common\enum\order\PayStatus;
use app\common\model\Order;
use think\facade\Db;
use think\facade\Log;

/**
 *求购订单
 */
class PurchaseOrderPay extends PaySuccess
{
    public function onSuccess(array $payment, int $payType): bool
    {
        Db::startTrans();
        try{
            // 订单更新
            $order = Order::where('order_no', $payment['order_no'])->find();
            $order->pay_status = PayStatus::SUCCESS;
            $order->trans_no = $payment['transaction_id'];
            $order->order_status = OrderStatus::COMPLETED;
            $order->pay_time = time();
            $order->save();

            //更新求购记录
            $purchase_goods = (new PurchaseRecords())->where('id',$order->order_source_id)->find();
            $purchase_goods->status = 1;
            $purchase_goods->save();


            Db::commit();
            return true;
        }catch (\Exception $e){
            Db::rollBack();
            Log::channel('wxpay')->info('订单号：'.$payment['order_no'].'支付失败');
            Log::channel('wxpay')->info('失败原因：'.$e->getMessage());
            return false;
        }

    }

    public function callSuccess()
    {

    }
}